package com.xyp.cityipgen.controller;

import com.xyp.cityipgen.common.excel.ExportExcel;
import com.xyp.cityipgen.dao.IpDataDao;
import com.xyp.cityipgen.model.CityIP;
import com.xyp.cityipgen.model.Resp;
import com.xyp.cityipgen.model.User;
import com.xyp.cityipgen.utils.DateFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xyp
 * @date 2019-08-07 16:34
 * @description
 * @version: 1.0
 */
@Controller
public class CityIPController {
    @Autowired
    private IpDataDao dataDao;

    @RequestMapping(value = "downloadExcel",method = RequestMethod.GET)
    public void downloadExcel(@RequestParam("count") Integer count,@RequestParam("city") String city, HttpServletResponse response){
        if(count>10){
            System.out.println("count 值不能大于10");
            return;
        }
        List<CityIP> cityIPS = dataDao.getListByWhere(city);
        String fileName = "IP地址.xlsx";
        List<User> list = DateFactory.getExcelData(cityIPS,count);
        try {
            new ExportExcel("IP地址", User.class, 2).setDataList(list).write(response, fileName).dispose();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
